/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "comet.h"

#include "gameengine.h"
#include <QDebug>

/*! \class Comet
    Comet game object.
*/

/*!
  Constructor for comet game object.

  \a frame Current frame number. When the GameObject was created.

  \a rewindEnabled Is the rewind mode enabled.

  \a startTime Start time of comet drawing.

  \a lengthChange Length change of the comet when flying by.

  \a startWidth Start width of the comet.

  \a widthChange Width change of the comet when flying by.
*/
Comet::Comet(int frame, bool rewindEnabled, FPtype startTime, FPtype lengthChange, 
             FPtype startWidth, FPtype widthChange)
    : GameObject(TypeComet, frame, GameEngine::GameObjectRadiusComet, SpeedLimitHigh, rewindEnabled, 
                 MassMedium, ScoreComet)
    , RotatingObject()
    , startTime(startTime)
    , startLength(GameEngine::GameObjectRadiusComet * 2.0f)
    , lengthChange(lengthChange)
    , startWidth(startWidth)
    , widthChange(widthChange)
{
    setRotationAngle(0.0f);
    setRotationSpeed(0.0f);
    setRotationAxis(0.0f, 0.0f, -1.0f);
}

/// Gets length of the comet at \a currentTime.
FPtype Comet::getLength(FPtype currentTime) const
{
    const FPtype timeDiff = currentTime - startTime;
    if (timeDiff < 0.0f)
        return startLength;
    else
        return startLength + lengthChange * timeDiff;
}

/// Gets width of the comet at \a currentTime.
FPtype Comet::getWidth(FPtype currentTime) const
{
    const FPtype timeDiff = currentTime - startTime;
    if (timeDiff < 0.0f)
        return startWidth;
    else
        return startWidth + widthChange * timeDiff;
}

/// Collision test for comet.
bool Comet::testCollision(FPtype x, FPtype y, FPtype currentTime)
{
    // the head test.

    const FPtype headX = posX();
    const FPtype headY = posY();

    const FPtype dx = x - headX;
    const FPtype dy = y - headY;

    const FPtype sum = dx * dx + dy * dy;
    if (sum < 100.0f)   // see also GameEngine::GameObjectRadiusComet
        return true;

    // the tail test.

    const FPtype angle = rotationAngle() + M_PI;

    const FPtype h2t_unitX = sin(angle);
    const FPtype h2t_unitY = cos(angle);

    const FPtype scalarProd = h2t_unitX * dx + h2t_unitY * dy;
    if (scalarProd < 0.0f)
        return false;

    const FPtype length = getLength(currentTime);

    if (sum > length * length)
        return false;

    const FPtype width = getWidth(currentTime);
    return (acos(scalarProd / sqrt(sum)) < tan(width / (length + length)));
}
